package com.crazypos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crazypos.dao.AttributeDao;
import com.crazypos.dao.AttributeValuesDao;
import com.crazypos.enums.AttributeStatus;
import com.crazypos.enums.YesOrNo;
import com.crazypos.exceptions.MyException;
import com.crazypos.pojo.AttributeEntity;
import com.crazypos.pojo.AttributeValuesEntity;
import com.crazypos.pojo.bo.AdminCreateAttributeBO;
import com.crazypos.pojo.bo.AdminCreateAttributeValueBO;
import com.crazypos.pojo.bo.AdminDeleteAttributeBO;
import com.crazypos.service.IAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttributeService extends ServiceImpl<AttributeDao, AttributeEntity> implements IAttributeService {

    @Autowired
    private AttributeDao attributeDao;

    @Autowired
    private AttributeValuesDao attributeValuesDao;

    @Override
    public void createAttribute(AdminCreateAttributeBO bo, String userId, String organizationId) {

        int isExist = attributeDao.selectCount(
                new QueryWrapper<AttributeEntity>()
                        .eq("code", bo.getCode())
                        .eq("organization_id", organizationId)
        );

        if (isExist > 0) {
            throw new MyException(500, "Code已经存在");
        }

        AttributeEntity attributeEntity = new AttributeEntity();
        attributeEntity.setName(bo.getName());
        attributeEntity.setCode(bo.getCode());
        attributeEntity.setCreatedBy(userId);
        attributeEntity.setStatus(AttributeStatus.NORMAL.getValue());
        attributeEntity.setIsSystem(YesOrNo.YES.getValue());
        attributeEntity.setOrganizationId(organizationId);

        attributeDao.insert(attributeEntity);
    }

    @Override
    public void deleteAttribute(AdminDeleteAttributeBO bo, String organizationId) {
        int isExist = attributeDao.selectCount(
                new QueryWrapper<AttributeEntity>()
                        .eq("id", bo.getId())
                        .eq("organization_id", organizationId)
        );

        if (isExist == 0) {
            throw new MyException(404, "Attribute不存在");
        }

        AttributeEntity entity = new AttributeEntity();
        entity.setId(bo.getId());
        entity.setStatus(AttributeStatus.DISABLED.getValue());
        attributeDao.updateById(entity);
    }

    @Override
    public void createAttributeValue(AdminCreateAttributeValueBO bo) {
        Integer attributeId = bo.getAttributeId();
        String value = bo.getValue();
        int isExist = attributeValuesDao.selectCount(
                new QueryWrapper<AttributeValuesEntity>()
                        .eq("attribute_id", attributeId)
                        .eq("value", value)
        );

        if (isExist > 0) {
            throw new MyException(500, "attribute value已经存在");
        }

        AttributeValuesEntity entity = new AttributeValuesEntity();
        entity.setName(bo.getName());
        entity.setValue(value);
        entity.setAttributeId(attributeId);

        attributeValuesDao.insert(entity);
    }

    @Override
    public List<AttributeEntity> getAttributeList(String organizationId) {
        return attributeDao.selectList(
                new QueryWrapper<AttributeEntity>()
                        .eq("organization_id", organizationId)
        );
    }

    @Override
    public List<AttributeValuesEntity> getValuesByAttributeId(Integer attributeId) {
        return attributeValuesDao.selectList(
                new QueryWrapper<AttributeValuesEntity>()
                        .eq("attribute_id", attributeId)
        );
    }
}
